//
//  SmallDecisionsHomeViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/6/14.
//

#import "SmallDecisionsHomeViewController.h"
#import "SmallDecisionsHomeModel.h"
#import "SmallDecisionsHomeCell.h"
#import "SmallDecisionsHomeHeaderReusableView.h"
#import "SmallDecisionsHomeTitleHeaderReusableView.h"
#import <StoreKit/StoreKit.h>

@interface SmallDecisionsHomeViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray  *list;

@end

@implementation SmallDecisionsHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"首页";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self collectionView];
    [self startLoadViewData];

#ifndef DEBUG
    [SKStoreReviewController requestReview];
#endif
}


- (void)startLoadViewData{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSString *path = [[NSBundle mainBundle] pathForResource:@"tools" ofType:@"json"];
        NSData  *data  = [NSData dataWithContentsOfFile:path];
        NSArray *list = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSMutableArray * mutList = [[NSMutableArray alloc] init];
            for (NSUInteger i = 0, n = list.count; i < n; i ++) {
                [mutList addObject:[NSArray yy_modelArrayWithClass:[SmallDecisionsHomeModel class] json:list[i]]];
            }
            self.list = [mutList copy];
            [self.collectionView reloadData];
        });
    });
}

- (UICollectionView *)collectionView {
    if(!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        CGFloat itemW = (kScreenWidth - 82) / 3.0;
        CGFloat itemH = itemW * 1.2;
        layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
        layout.itemSize = CGSizeMake(itemW, itemH);
        layout.minimumInteritemSpacing = 20;
        layout.minimumLineSpacing = 20; // cell 间距
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [self.view addSubview:_collectionView];
        [_collectionView registerClass:[SmallDecisionsHomeCell class] forCellWithReuseIdentifier:@"SmallDecisionsHomeCellId"];
        [_collectionView registerClass:[SmallDecisionsHomeHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SmallDecisionsHomeHeaderReusableViewId"];
        [_collectionView registerClass:[SmallDecisionsHomeTitleHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SmallDecisionsHomeTitleHeaderReusableViewId"];
        
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_offset(0);
        }];
    }
    return  _collectionView;
}


#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.list.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSArray<SmallDecisionsHomeModel *> *list = self.list[section];
    return list.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SmallDecisionsHomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallDecisionsHomeCellId" forIndexPath:indexPath];
    NSArray<SmallDecisionsHomeModel *> *list = self.list[indexPath.section];
    SmallDecisionsHomeModel *model = list[indexPath.item];
    cell.model = model;
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSArray<SmallDecisionsHomeModel *> *list = self.list[indexPath.section];
    SmallDecisionsHomeModel *model = list[indexPath.item];
    SmallDecisionsBaseViewController *baseVC = [[NSClassFromString(model.router) alloc] init];
    [SmallDecisionsNavigatorManager pushViewController:baseVC];
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        if(indexPath.section == 0) {
            SmallDecisionsHomeHeaderReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SmallDecisionsHomeHeaderReusableViewId" forIndexPath:indexPath];
            UITapGestureRecognizer *panGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCollectionViewHeaderViewAction)];
            [headerView addGestureRecognizer:panGes];
            return headerView;
        } else {
            SmallDecisionsHomeTitleHeaderReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SmallDecisionsHomeTitleHeaderReusableViewId" forIndexPath:indexPath];
            return headerView;
        }
    }
    return nil;
}
// 根据需要设置不同的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if(section == 0) {
        return CGSizeMake(kScreenWidth, kScreenWidth*0.35);
    }
    return CGSizeMake(kScreenWidth, 40);
}


- (void)tapCollectionViewHeaderViewAction {
    SmallDecisionsBaseViewController *baseVC = [[NSClassFromString(@"SmallDecisionsSzSetViewController") alloc] init];
    [SmallDecisionsNavigatorManager pushViewController:baseVC];
}


@end
